<?php
/**
 * @version  1.0
 * @Project  D64-LSR-STOPPER
 * @author   Lars Echterhoff, Dennis Morhardt
 * @package
 * @copyright Copyright (C) 2012 Dennis Morhardt, Echterhoff Medientechnik
 * @license  http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL version 2
 * @description Mit diesem Plugin verhinderst du ungewollte Verlinkungen zu Medien, welche das Leitungsschutzrecht unterstützen bzw. in Anspruch nehmen.
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');
jimport('joomla.plugin.plugin');

/**
 * Define blacklist update URL
 */
define("LSR_BLACKLIST_URL", "http://leistungsschutzrecht-stoppen.d-64.org/blacklist.txt");

/**
 * Load Simple HTML DOM
 */
require_once dirname(__FILE__) . '/simple_html_dom.php';

class plgContentD64LsrStopper extends JPlugin
{
	function plgContentD64LsrStopper(&$subject, $params)
	{
		parent::__construct($subject, $params);
		$this->_plugin =& JPluginHelper::getPlugin( 'content', 'd64lsrstopper' );
	}

	function onContentBeforeDisplay($context, &$article, &$params, $page=0)
	{
		$this->d64_lsr_update_blacklist();
		$article->introtext = $this->d64_lsr_check($article->introtext);
		$article->text = $this->d64_lsr_check($article->text);
	}

	/**
	 * Function to update the local blacklist storage
	 */
	function d64_lsr_update_blacklist() {
		// Get current blacklist parameter
		$blacklist = $this->params;

		$blacklist->set('cachefile', dirname(__FILE__).'/blacklist.txt');

		if(file_exists($blacklist->get('cachefile')))
			$blacklist->set('lastupdate',filemtime($blacklist->get('cachefile')));

		// Check if blacklist exists or is outdated
		if ( !file_exists($blacklist->get('cachefile')) || (time()-( 60 * 60 * 6 )) > $blacklist->get('lastupdate') ){
			// Download new blacklist
			$response = file_get_contents(LSR_BLACKLIST_URL);

			// Check for errors
			if ( $response !== false || $response != "" ){
				// Save new blacklist
				if($blacklist->get('cachefile')!="") file_put_contents($blacklist->get('cachefile'),$response);

			}
		}
	}

	/**
	 * Check string for link to blacklisted sites and replace them
	 */
	function d64_lsr_check($string) {
		// Get DOM for string
		$dom = str_get_html($string);

		// Get blacklist
		$blacklist = $this->params;
		$blacklistSites = array_filter(array_map('trim', explode(",",file_get_contents($blacklist->get('cachefile')))));

		// Check if blacklist has entries
		if ( false == is_array( $blacklistSites ) || 0 == count( $blacklistSites ) )
			return $string;

		// Prepare regex
		$regex = "^(" . implode( "|", $blacklistSites ) . ")^";

		// Find all links
		foreach( $dom->find('a') as $element ):
			// Check if it's external link
			if ( 'http' != substr( $element->href, 0, 4 ) )
				continue;

			// Get domain
			$parts = parse_url($element->href);

			// Check for blacklisted domain
			if ( preg_match( $regex, strtolower( $parts["host"] ) ) )
				$element->href = "http://leistungsschutzrecht-stoppen.d-64.org/blacklisted/?url=" . base64_encode($element->href);
		endforeach;

		// Return checked string
		return $dom->save();
	}

	private function InjectHeadCode()
	{
		//Currently no use and just for remind purposes still in here
		return;
	}

}
